import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import ctrFinanceiro.bd.UsuarioBD;
import ctrFinanceiro.ctr.CtrFinanceiroCTR;
import ctrFinanceiro.gui.UsuarioLogado;

public class CtrFinanceiro extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private CtrFinanceiroCTR ctrFinanceiroCTR; // @jve:decl-index=0:

	private JDesktopPane jDesktopPane = null;
	// private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenuCadastro = null;
	private JMenu jMenuRelatorio = null;
	private JMenu jMenuSair = null;
	private JMenuItem jMenuItemCategoria = null;
	private JMenuItem jMenuItemConta = null;
	private JMenuItem jMenuItemMovimento = null;
	private UsuarioBD usuarioBD = null;

	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenuCadastro());
			jJMenuBar.add(getJMenuRelatorio());
			jJMenuBar.add(getJMenuSair());
			jJMenuBar.setOpaque(true);
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenuCadastro
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuCadastro() {
		if (jMenuCadastro == null) {
			jMenuCadastro = new JMenu("Cadastro");
			jMenuCadastro.add(getJMenuItemCategoria());
			jMenuCadastro.add(getJMenuItemConta());
			jMenuCadastro.add(getJMenuItemMovimento());
		}
		return jMenuCadastro;
	}

	private JMenuItem getJMenuItemCategoria() {
		if (jMenuItemCategoria == null) {
			jMenuItemCategoria = new JMenuItem("Categoria");
			jMenuItemCategoria.addActionListener(this);
		}
		return jMenuItemCategoria;
	}

	private JMenuItem getJMenuItemConta() {
		if (jMenuItemConta == null) {
			jMenuItemConta = new JMenuItem("Conta");
			jMenuItemConta.addActionListener(this);
		}
		return jMenuItemConta;
	}

	private JMenuItem getJMenuItemMovimento() {
		if (jMenuItemMovimento == null) {
			jMenuItemMovimento = new JMenuItem("Movimento");
			jMenuItemMovimento.addActionListener(this);
		}
		return jMenuItemMovimento;
	}

	/**
	 * This method initializes jMenuRelatorio
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuRelatorio() {
		if (jMenuRelatorio == null) {
			jMenuRelatorio = new JMenu();
			jMenuRelatorio.setText("Relatórios");

		}
		return jMenuRelatorio;
	}

	/**
	 * This method initializes jMenuSair
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuSair() {
		if (jMenuSair == null) {
			jMenuSair = new JMenu("Sair");
		}
		return jMenuSair;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CtrFinanceiro thisClass = new CtrFinanceiro();
		thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		thisClass.setResizable(false);
		thisClass.setVisible(true);
		UsuarioLogado.getInstance().setUsuario(
				thisClass.usuarioBD.getUsuarioPorId(1));
	}

	/**
	 * This is the default constructor
	 */
	public CtrFinanceiro() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.ctrFinanceiroCTR = new CtrFinanceiroCTR();
		int inset = 16;
		usuarioBD = new UsuarioBD();

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setBounds(inset, inset, screenSize.width - inset * 2,
				screenSize.height - inset * 2);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJDesktopPane());
		this.setTitle("Controle Financeiro");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	public JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			jDesktopPane = new JDesktopPane();
			jDesktopPane.setLayout(null);
		}
		return jDesktopPane;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == getJMenuItemCategoria()) {
			getJDesktopPane().add(ctrFinanceiroCTR.loadCategoriaGUI());
		} else if (e.getSource() == getJMenuItemConta()) {
			getJDesktopPane().add(ctrFinanceiroCTR.loadContaGUI());
		} else if (e.getSource() == getJMenuItemMovimento()) {
			getJDesktopPane().add(ctrFinanceiroCTR.loadMovimentoGUI());
		} else if (e.getSource() == getJMenuSair()) {
			System.exit(0);
		}
	}
}
